﻿
using System ;

using MskEdit.Dxf.Model ;
using MskEdit.Sdk ;


namespace MskEdit.Dxf.Backend
{
    internal class DxfSymbolTableReader
    {   
                
        internal static DxfTable TryRead( string[] allLines, ref int lineNo, DxfTable fallbackTable ) {
        
            int internalLineNo = lineNo ;


            if ( DxfLineReader.TryRead( "0", "TABLE", allLines, ref internalLineNo, null ) == null ) 
                return fallbackTable ;
                
            DxfTable DxfTable = new DxfTable( allLines , new SdkSubrange(0,0)) ;
/*                
            DxfTable.TableName = DxfStringReader.TryRead( "2", allLines, ref internalLineNo, null ) ; 
            if ( DxfTable.TableName == null ) {
                SdkDiagnostics.TraceWarning( "Could not read table name", internalLineNo ) ;
                return fallbackTable ;
            }
 */               
            DxfTable.Handle = DxfStringReader.TryRead( "5", allLines, ref internalLineNo, null ) ; 
            if ( DxfTable.Handle == null ) {
                SdkDiagnostics.TraceWarning( "Could not read class cpp name", internalLineNo ) ;
                return fallbackTable ;
            }
                
            DxfTable.OwnerPointer = DxfStringReader.TryRead( "330", allLines, ref internalLineNo, null ) ; 
            if ( DxfTable.OwnerPointer == null ) {
                SdkDiagnostics.TraceWarning( "Could not read wner pointer", internalLineNo ) ;
                return fallbackTable ;
            }
                
            DxfTable.SubclassMarker = DxfStringReader.TryRead( "100", allLines, ref internalLineNo, null ) ; 
            if ( DxfTable.SubclassMarker == null ) {
                SdkDiagnostics.TraceWarning( "Could not read subclass marker", internalLineNo ) ;
                return fallbackTable ;
            }
                
            DxfTable.MaxEntries = DxfShortReader.TryRead( "70", allLines, ref internalLineNo, null ) ; 
            if ( ! DxfTable.MaxEntries.HasValue ) {
                SdkDiagnostics.TraceWarning( "Could not read max entries", internalLineNo ) ;
                return fallbackTable ;
            }
            
            lineNo = internalLineNo ;
            
            return DxfTable ;
        }

    }
}
